﻿using System.Collections.Generic;
using Application.Core.Interfaces;

namespace Application.Core.Entities
{
    /// <summary>
    /// 分类
    /// </summary>
    public class Category : BaseEntity
    {
        private ICategoryService _categoryService;
        public Category(ICategoryService categoryService)
        {
            this._categoryService = categoryService;
        }

        /// <summary>
        /// 分类ID
        /// </summary>
        public string CategoryId { get; set; }

        /// <summary>
        /// 父级分类ID
        /// </summary>
        public string ParentCategoryId { get; set; }

        /// <summary>
        /// 分类名称
        /// </summary>
        public string CategoryName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public long CreateTime { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        public int SortId { get; set; }

        /// <summary>
        /// 当前节点下文档数量
        /// </summary>
        public long DocumentCount { get; set; }

        /// <summary>
        /// 总文档数量
        /// </summary>
        public long TotalDocumentCount { get; set; }

        public bool IsRootCategory()
        {
            return false;
        }

        /// <summary>
        /// 是否有子类
        /// </summary>
        /// <returns></returns>
        public bool HasChild()
        {
            return _categoryService.HasChild(this);
        }

        /// <summary>
        /// 是否包含指定子类
        /// </summary>
        /// <param name="childCategoryId"></param>
        /// <returns></returns>
        public bool HasChild(Category queryChildCategory)
        {
            List<Category> childCategories = GetAllChild();
            return childCategories.Exists(
                childCategory => 
                childCategory.CategoryId.Equals(queryChildCategory.CategoryId) || 
                childCategory.CategoryName.Equals(queryChildCategory.CategoryName)
            );
        }

        /// <summary>
        /// 获取所有的子类
        /// </summary>
        /// <returns></returns>
        public List<Category> GetAllChild()
        {
            List < Category > childs = new List<Category>();
            if (HasChild())
            {
                childs = _categoryService.GetChildCategories();
            }
            return childs;
        }

        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <returns></returns>
        public bool AddChild(Category category)
        {
            if (category.ParentCategoryId != CategoryId)
            {
                return false;
            }

            if (HasChild(category))
            {
                return false;
            }

            return _categoryService.AddCategory(category);
        }

        /// <summary>
        /// 移除子节点
        /// </summary>
        /// <returns></returns>
        public bool RemoveChild(Category childCategory)
        {
            if (childCategory == null)
            {
                return false;
            }

            if (!HasChild(childCategory))
            {
                return false;
            }

            _categoryService.RemoveCategory(childCategory);
            return true;
        }

        /// <summary>
        /// 获取当前节点下所有文档
        /// </summary>
        /// <returns></returns>
        public List<Document> GetAllDocuments()
        {
            return _categoryService.GetAllDocuments(this);
        }
    }
}
